//
// Created by LONG on 2020/10/18.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include "../lib/doctest.h"
#include "../../include/utils/memPoolSet.h"

using namespace std;

TEST_SUITE ("MEM_POOL_TS")
{
    TEST_CASE ("MEM_POOL_TC")
    {
        int32_t rc;
        uint32_t loop1;
        uint32_t loop2;
        uint32_t loop3;
        void *memBlock;
        vector<void *> ptrVec;

        SUBCASE("MEM_POOL_SC_BASE")
        {
            S_MemPoolAttr memPoolAttrTable1[] =
            {
                { MP_SIZE(128), MP_COUNT(256)},
                { MP_SIZE(256), MP_COUNT(128)},
                { MP_SIZE(512), MP_COUNT(128)},
                { MP_SIZE(1024), MP_COUNT(64)},
                { MP_SIZE(2048), MP_COUNT(32)},
                { MP_SIZE(4096), MP_COUNT(16)},
                { MP_SIZE(8192), MP_COUNT(8)},
                { MP_SIZE(16384), MP_COUNT(4)}
            };
            S_MemPoolAttr memPoolAttrTable2[] =
            {
                { MP_SIZE(8), MP_COUNT(256)},
                { MP_SIZE(16), MP_COUNT(128)},
                { MP_SIZE(24), MP_COUNT(128)},
                { MP_SIZE(32), MP_COUNT(64)},
                { MP_SIZE(40), MP_COUNT(32)},
                { MP_SIZE(48), MP_COUNT(16)},
                { MP_SIZE(56), MP_COUNT(8)},
                { MP_SIZE(64), MP_COUNT(4)}
            };
            S_MemPoolAttr memPoolAttrTable3[] =
            {
                { MP_SIZE(8), MP_COUNT(256)},
                { MP_SIZE(13), MP_COUNT(128)},
                { MP_SIZE(20), MP_COUNT(128)},
                { MP_SIZE(70), MP_COUNT(64)},
                { MP_SIZE(90), MP_COUNT(32)},
                { MP_SIZE(100), MP_COUNT(16)},
                { MP_SIZE(105), MP_COUNT(8)},
                { MP_SIZE(108), MP_COUNT(4)}
            };

            S_MemPoolSetAttr memPoolSetAttrTable[] =
            {
                { MEM_POOL_TYPE_MPMC, MP_MODE_POWER2, ARRAY_SIZE(memPoolAttrTable1), memPoolAttrTable1},
                { MEM_POOL_TYPE_MPMC, MP_MODE_MULT, ARRAY_SIZE(memPoolAttrTable2), memPoolAttrTable2},
                { MEM_POOL_TYPE_MPMC, MP_MODE_NORMAL, ARRAY_SIZE(memPoolAttrTable3), memPoolAttrTable3},
            };

            rc = MPS_Init(memPoolSetAttrTable, ARRAY_SIZE(memPoolSetAttrTable));
            REQUIRE(CSP_RC_OK == rc);
            for (loop1 = 0; loop1 < ARRAY_SIZE(memPoolSetAttrTable); loop1++)
            {
                for (loop2 = 0; loop2 < memPoolSetAttrTable[loop1].memPoolAttrNum; loop2++)
                {
                    for (loop3 = 0; loop3 < memPoolSetAttrTable[loop1].pMemPoolAttrs[loop2].count; loop3++)
                    {
                        memBlock = MPS_Get(loop1, memPoolSetAttrTable[loop1].pMemPoolAttrs[loop2].size, NULL, 0);
                        REQUIRE(memBlock);
                        ptrVec.push_back(memBlock);
                    }
                }
            }
            for (auto memBlock : ptrVec)
            {
                rc = MPS_Put(memBlock);
                REQUIRE(CSP_RC_OK == rc);
            }
            MPS_Final();
            REQUIRE(OS_MemGetAllocCount() == OS_MemGetFreeCount());
            REQUIRE(MPS_GetCount() == MPS_PutCount());
        }
    }
}
